<script setup lang="ts">
import { get, loadFile, message, post } from '@/utils'
import { Market, MarketStatus } from '@/enum/market'
import Modal from './components/Modal.vue'
import useModal from '@/hooks/useModal'
import { BtnPermission } from '@/enum/permission'
import useTable from '@/hooks/useTable'
const router = useRouter()
const { store } = useModal()
const { formatColumns } = useTable()
const searchQuery = reactive({
  type: Market.限时折扣,
})

const formRef = ref()
const actionRef = ref()
const pagination = {
  showPageSize: true,
  pageSize: 20,
}
const request = (params: any, sort?, filters?): Promise<any> => {
  return new Promise((resolve, reject) => {
    get(
      {
        url: '/server-shop/sys-seller/publicity/page',
        data: { size: params.pageSize, ...params },
      },
      ({ data, code }) => {
        resolve({
          data: data.records,
          total: data.total,
          success: code == 200,
        })
      }
    )
  })
}
const columns = formatColumns([
  { title: '活动ID', dataIndex: 'id', width: 200 },
  { title: '标题', dataIndex: 'title', hideInSearch: false, width: 200 },
  { title: '排序', dataIndex: 'sort', width: 200 },
  { title: '开始时间', dataIndex: 'beginTime', width: 200 },
  { title: '结束时间', dataIndex: 'endTime', width: 200 },
  {
    title: '优惠券',
    dataIndex: 'isCoupon',
    valueEnum: {
      1: { text: '启用', status: 'Success' },
      0: { text: '禁用', status: 'Error' },
    },
    width: 200,
  },
  // { title: '状态', dataIndex: 'status' },
  // { title: '配置', dataIndex: 'config', width: 200 },
  {
    title: '创建时间',
    dataIndex: 'createTime',

    width: 200,
  },

  {
    title: '操作',
    dataIndex: 'option',
    width: 250,
    fixed: 'right',
    slotName: 'option',
    btnList: [
      {
        name: '编辑',
        permission: BtnPermission.编辑,
        handle: ({ record }) => store.handleShow(record),
      },
      {
        permission: BtnPermission.编辑,
        name: '管理活动商品',
        handle: ({ record }) => {
          router.push({
            name: 'marketActivityGoods',
            query: { publicityId: record.id, type: Market.限时折扣 },
          })
        },
      },
      {
        name: '删除',
        type: 'popconfirm',
        permission: BtnPermission.删除,
        tip: '确认删除该活动吗?',
        handle: ({ record }) => {
          post({ url: '/server-shop/sys-seller/publicity/remove?ids=' + record.id }, ({ code, msg }) => {
            if (code == 200) actionRef.value.reload()

            message({
              type: code == 200 ? 'success' : 'error',
              content: msg,
            })
          })
        },
      },
    ],
  },
])
</script>

<template>
  <div class="page">
    <ProTable :columns="columns" :request="request" row-key="id" :form-ref="ref => (formRef = ref)" :scroll="{ x: 1000 }" :pagination="pagination" header-title="" column-resizable :params="searchQuery" :bordered="{ cell: true }" :action-ref="ref => (actionRef = ref)">
      <template #option-render="{ dom, reset }">
        <component :is="dom[1]" />
        <component :is="dom[0]" />
        <a-button type="primary" @click="store.handleShow({})" v-permission="[BtnPermission.新建]">
          <template #icon>
            <icon-plus />
          </template>
          新建
        </a-button>
      </template>
    </ProTable>
    <Modal v-if="store.show" v-model:show="store.show" :id="store.id" :type="Market.限时折扣" @confirm="actionRef.reload" />
  </div>
</template>
